package com.forum.module.blog.service.article_tag;

import com.alibaba.nacos.shaded.com.google.common.annotations.VisibleForTesting;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.forum.framework.common.exception.util.ServiceExceptionUtils;
import com.forum.framework.common.pojo.PageResult;
import com.forum.framework.common.util.object.BeanUtils;
import com.forum.module.blog.controller.article_tag.vo.ArticleTagPageReqVO;
import com.forum.module.blog.controller.article_tag.vo.ArticleTagSaveReqVO;
import com.forum.module.blog.dal.dataobject.article_tag.ArticleTagDO;
import com.forum.module.blog.dal.mysql.article_tag.ArticleTagMapper;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.forum.module.system.enums.ErrorCodeConstants.CATEGORY_TAG_NOT_EXISTS;

/**
 * 功能:
 * 作者:ouyzh
 * 日期:2025/3/2 18:25
 */
@Service
public class ArticleTagServiceImpl extends ServiceImpl<ArticleTagMapper, ArticleTagDO> implements ArticleTagService {
    private final ArticleTagMapper articleTagMapper;

    public ArticleTagServiceImpl(ArticleTagMapper articleTagMapper) {
        this.articleTagMapper = articleTagMapper;
    }

    @Override
    public Long createArticleTag(ArticleTagSaveReqVO reqVO) {
        ArticleTagDO articleTagDO = BeanUtils.toBean(reqVO, ArticleTagDO.class);
        articleTagMapper.insert(articleTagDO);
        return articleTagDO.getId();
    }

    @Override
    public void updateArticleTag(ArticleTagSaveReqVO reqVO) {
        ArticleTagDO updateObj = BeanUtils.toBean(reqVO, ArticleTagDO.class);
        articleTagMapper.insert(updateObj);
    }

    @Override
    public void deleteById(Long id) {
        // 1. 校验文章标签存在
        validateArticleTagExists(id);
    }

    @Override
    public PageResult<ArticleTagDO> getArticleTagPage(ArticleTagPageReqVO pageReqVO) {
        return articleTagMapper.selectPage(pageReqVO);
    }

    @Override
    public ArticleTagDO getArticleTag(Long id) {
        return articleTagMapper.selectById(id);
    }

    @VisibleForTesting
    ArticleTagDO validateArticleTagExists(Long id) {
        if (id == null) {
            return null;
        }
        ArticleTagDO articleTagDO = articleTagMapper.selectById(id);
        if (articleTagDO == null) {
            throw ServiceExceptionUtils.exception(CATEGORY_TAG_NOT_EXISTS);
        }
        return articleTagDO;
    }


}
